<template>
  <div class="data-table-container layout-padding">
    <el-card class="layout-padding-auto" shadow="hover">
      <el-form ref="queryFormRef" :model="state.tableData.param.params" inline>
        <el-form-item class="!mr-4" prop="uid">
          <el-input v-model="state.tableData.param.params.uid"
                    placeholder="请输入订单号" size="default"/>
        </el-form-item>
        <el-form-item class="!mr-4" prop="payType" style="width: 11%">
          <el-select v-model="state.tableData.param.params.payType"
                     clearable
                     placeholder="请选择支付方式"
                     size="default">
            <el-option
                v-for="item in state.payTypeList"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item class="!mr-4" prop="payType" style="width: 11%">
          <el-select v-model="state.tableData.param.params.payStatus"
                     clearable
                     placeholder="请选择支付状态"
                     size="default">
            <el-option
                v-for="item in state.payStatusList"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item v-auth="'user.list'" class="!mr-4">
          <el-button size="default" type="primary" @click="getTableData">
            <el-icon>
              <ele-Search/>
            </el-icon>
            查询
          </el-button>
        </el-form-item>
        <el-form-item v-auth="'user.list'" class="!mr-4">
          <el-button size="default" type="primary" @click="onHandleResetFields">
            <el-icon>
              <ele-Refresh/>
            </el-icon>
            重置
          </el-button>
        </el-form-item>
      </el-form>

      <!--显示字段-->
      <el-row :gutter="10" class="mb8">
        <right-toolbar :columns="state.tableData.columns" @queryTable="getTableData"></right-toolbar>
      </el-row>
      <el-table v-loading="state.tableData.loading" :data="state.tableData.data" highlight-current-row
                style="width: 100%"
                @sort-change="handleSortChange">
        <el-table-column v-if="state.tableData.columns[0].visible" label="订单号" prop="uid" show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[13].visible" label="会员名称" prop="memberName"
                         show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[1].visible" label="数量" prop="quantity" show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[2].visible" label="活动名称" prop="actName"
                         show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[3].visible" label="活动金额" prop="actAmount"
                         show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[4].visible" label="应付金额" prop="amount"
                         show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[5].visible" label="支付金额" prop="payAmount"
                         show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[6].visible" label="成本金额" prop="costAmount"
                         show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[7].visible" label="利润金额" prop="profitAmount"
                         show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[8].visible" label="抹零金额" prop="roundDown"
                         show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[9].visible" label="余额金额" prop="balanceAmount"
                         show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[10].visible" label="支付时间" prop="payDate"
                         show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[11].visible" label="支付方式" prop="payType"
                         show-overflow-tooltip>
          <template #default="scope">
            <dict-tag :options="payment_type" :value="scope.row.payType"></dict-tag>
          </template>
        </el-table-column>
        <el-table-column v-if="state.tableData.columns[25].visible" label="第三方订单号" prop="paymentNo"
                         show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[12].visible" label="支付状态" prop="payStatus"
                         show-overflow-tooltip>
          <template #default="scope">
            <dict-tag :options="pay_status" :value="scope.row.payStatus"></dict-tag>
          </template>
        </el-table-column>
        <el-table-column v-if="state.tableData.columns[14].visible" label="优惠券名称" prop="couponNames"
                         show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[15].visible" label="会员优惠金额" prop="memberDiscount"/>
        <el-table-column v-if="state.tableData.columns[16].visible" label="销售渠道" prop="channel"/>
        <el-table-column v-if="state.tableData.columns[17].visible" label="订单状态" prop="status"/>
        <el-table-column v-if="state.tableData.columns[18].visible" label="发票状态" prop="invoiceStatus"/>
        <el-table-column v-if="state.tableData.columns[19].visible" label="销售员" prop="userNames"/>
        <el-table-column v-if="state.tableData.columns[20].visible" label="退款时间" prop="refundDate"/>
        <el-table-column v-if="state.tableData.columns[21].visible" label="商品名称" prop="commodityNames"/>
        <el-table-column v-if="state.tableData.columns[22].visible" label="优惠详情" prop="discountDetails"/>
        <el-table-column v-if="state.tableData.columns[23].visible" label="积分数量" prop="pointAmount"/>
        <el-table-column v-if="state.tableData.columns[24].visible" label="备注" prop="remark"/>
      </el-table>
      <el-pagination v-model:current-page="state.tableData.param.pageNum"
                     v-model:page-size="state.tableData.param.pageSize" :page-sizes="[10, 20, 30]" :pager-count="5"
                     :total="state.tableData.total" background class="mt15"
                     layout="total, sizes, prev, pager, next, jumper"
                     @size-change="onHandleSizeChange" @current-change="onHandleCurrentChange">
      </el-pagination>
    </el-card>
    <serverDialog ref="serverDialogRef" @refresh="getTableData()"/>
  </div>
</template>

<script lang="ts" name="systemUser" setup>
import {defineAsyncComponent, getCurrentInstance, onMounted, reactive, ref} from 'vue';
import DictTag from "/@/components/DictTag/index.vue";
import {saleStoreApi} from "/@/api/sale/commodity";
import {dictDataApi} from "/@/api/sys/dictData";

const {proxy} = getCurrentInstance();
const {pay_status, payment_type} = proxy.useDict("pay_status", "payment_type");

// 引入组件
const serverDialog = defineAsyncComponent(() => import('/src/views/store/server/dialog.vue'));

// 定义变量内容
const serverDialogRef = ref();
const queryFormRef = ref();
const state = reactive<SysUserState>({
  tableData: {
    data: [],
    total: 0,
    loading: false,
    param: {
      pageNum: 1,
      pageSize: 10,
      params: {
        uid: '',
        payType: '',
        payStatus: '',
      },
    },
    columns: [
      {key: 0, label: `订单号`, visible: true},
      {key: 1, label: `数量`, visible: false},
      {key: 2, label: `活动名称`, visible: false},
      {key: 3, label: `活动金额`, visible: false},
      {key: 4, label: `应付金额`, visible: true},
      {key: 5, label: `支付金额`, visible: true},
      {key: 6, label: `成本金额`, visible: true},
      {key: 7, label: `利润金额`, visible: true},
      {key: 8, label: `抹零金额`, visible: true},
      {key: 9, label: `余额金额`, visible: true},
      {key: 10, label: `支付时间`, visible: true},
      {key: 11, label: `第三方订单号`, visible: false},
      {key: 12, label: `支付状态`, visible: false},
      {key: 13, label: `会员名称`, visible: true},
      {key: 14, label: `优惠券名称`, visible: false},
      {key: 15, label: `会员优惠金额`, visible: false},
      {key: 16, label: `销售渠道`, visible: false},
      {key: 17, label: `订单状态`, visible: false},
      {key: 18, label: `发票状态`, visible: false},
      {key: 19, label: `销售员`, visible: false},
      {key: 20, label: `退款时间`, visible: false},
      {key: 21, label: `商品名称`, visible: false},
      {key: 22, label: `优惠详情`, visible: false},
      {key: 23, label: `积分数量`, visible: false},
      {key: 24, label: `备注`, visible: false},
      {key: 25, label: `第三方订单号`, visible: false},
    ],
  },
  payStatusList: [],
  payTypeList: [],
});


const handleSortChange = (data: any) => {
  if (data.order) {
    if (data.order.indexOf('asc') > -1) {
      state.tableData.param.sortOrder = 'asc'
    } else {
      state.tableData.param.sortOrder = 'desc'
    }
    state.tableData.param.sortName = data.prop
  }
  getTableData();
};

// 初始化表格数据
const getTableData = () => {
  state.tableData.loading = true;
  saleStoreApi()
      .getTableData(state.tableData.param)
      .then((res) => {
        state.tableData.data = res.data.records;
        state.tableData.total = res.data.total;
        state.tableData.loading = false;
      })
      .catch(() => {
        state.tableData.loading = false;
      });
};
// 分页改变
const onHandleSizeChange = (val: number) => {
  state.tableData.param.pageSize = val;
  getTableData();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
  state.tableData.param.pageNum = val;
  getTableData();
};
// 页面加载时
onMounted(() => {
  getTableData();

  // 支付状态
  dictDataApi()
      .getDictByType("pay_status")
      .then(res => {
        state.payStatusList = res.data;
      })

  // 支付类型
  dictDataApi()
      .getDictByType("payment_type")
      .then(res => {
        state.payTypeList = res.data;
      })

});
// 重置查询表单
const onHandleResetFields = () => {
  queryFormRef.value.resetFields();
  getTableData();
};
</script>
